https://leetcode.cn/problems/count-submatrices-with-top-left-element-and-sum-less-than-k/


class Solution {
public:
    int countSubmatrices(vector<vector<int>>& grid, int k) {


        int sum[1010][1010] = { 0 }, cnt = 0;
        for (int i = 1; i <= grid.size(); i++)
        {
            for (int j = 1; j <= grid[i - 1].size(); j++)
            {
                sum[i][j] = sum[i][j - 1] + sum[i - 1][j] - sum[i - 1][j - 1] + grid[i - 1][j - 1];

                if (sum[i][j] <= k)
                    cnt++;

            }
        }

        return cnt;
    }
};